package com.ymatou.userregister.testcase;

import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.userregister.parameter.BindEmailPhoneBean;
import com.ymatou.userregister.service.BindEmailCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.UserCallService;
import com.ymttest.database.mongo.ULC201406Bean;
import com.ymttest.database.mongo.ULC201406Wapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;

public class Ts_BindEmail {
    private static BindEmailPhoneBean bindEmailBean;
    private static BindEmailCall bindEmailCall;
    private ULC201406Wapper userWapper;
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("绑定用户邮箱测试");
	}

	@Before
	public void caseUp() throws Exception {
		bindEmailBean = new BindEmailPhoneBean();
		bindEmailCall = new BindEmailCall();
		userWapper = new ULC201406Wapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(bindEmailCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BindEmail_001() {
		Logger.start(true,"绑定登陆邮箱成功");
		try {
			String email = TestDataManager.getRandomNum(7)+"@bindemail.com";
			Map user = UserCallService.registerUser(null, TestDataManager.getPhoneNum(),null);
			
			bindEmailBean.setAction("BindLoginMail");
			bindEmailBean.setActionSource("1");
			bindEmailBean.setLoginId(user.get("loginId").toString());
			bindEmailBean.setLoginPassword("123456");
			bindEmailBean.setMail(email);
			bindEmailBean.setUserId((Integer)user.get("userId"));
			bindEmailCall.setData(bindEmailBean);
			bindEmailCall.callService();
			
			Logger.verifyEquals(ECode.SUCCESS, bindEmailCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("true", bindEmailCall.getString("Success"), "验证Success");
			Logger.verifyEquals("14", bindEmailCall.getString("Result"), "验证凭据（14表示操作成功）");
			ULC201406Bean users = userWapper.getULCInfoByUid((Integer)user.get("userId"));
			Logger.verifyEquals(email, users.getlBills().getEmail(), "验证邮箱信息");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BindEmail_002() {
		Logger.start(true,"修改登陆邮箱成功");
		try {
			String email = TestDataManager.getRandomNum(7)+"@bindemail.com";
			Map user = UserCallService.registerUser(email, TestDataManager.getPhoneNum(),null);
			
			String newEmail = TestDataManager.getEmail();
			bindEmailBean.setAction("ModifyLoginMail");
			bindEmailBean.setActionSource("1");
			bindEmailBean.setLoginId(user.get("loginId").toString());
			bindEmailBean.setLoginPassword("123456");
			bindEmailBean.setMail(newEmail);
			bindEmailBean.setUserId((Integer)user.get("userId"));
			bindEmailCall.setData(bindEmailBean);
			bindEmailCall.callService();
			
			Logger.verifyEquals(ECode.SUCCESS, bindEmailCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("true", bindEmailCall.getString("Success"), "验证Success");
			Logger.verifyEquals("14", bindEmailCall.getString("Result"), "验证凭据（14表示操作成功）");
			ULC201406Bean users = userWapper.getULCInfoByUid((Integer)user.get("userId"));
			Logger.verifyEquals(newEmail.toLowerCase(), users.getlBills().getEmail(), "验证邮箱信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BindEmail_003() {
		Logger.start(false,"不输入操作，失败");
		try {
			String email = TestDataManager.getRandomNum(4)+"@bindemail.com";
			Map user = UserCallService.registerUser(null, TestDataManager.getPhoneNum(),null);
			
			bindEmailBean.setActionSource("1");
			bindEmailBean.setLoginId(user.get("loginId").toString());
			bindEmailBean.setLoginPassword("123456");
			bindEmailBean.setMail(email);
			bindEmailBean.setUserId((Integer)user.get("userId"));
			bindEmailCall.setData(bindEmailBean);
			bindEmailCall.callService();
			
			Logger.verifyEquals(ECode.ERROR400, bindEmailCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("false", bindEmailCall.getString("Success"), "验证Success");
			Logger.verifyEquals("dto验证失败", bindEmailCall.getString("LastErrorMessage"), "验证Msg信息");
			Logger.verifyEquals("20", bindEmailCall.getString("Result"), "验证凭据（20表示错误的DTO数据结构）");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BindEmail_004() {
		Logger.start(false,"绑定已绑定过的邮箱，失败");
		try {
			ULC201406Bean existUser = userWapper.getULCInfoByUid(100);
			Map user = UserCallService.registerUser(null, TestDataManager.getPhoneNum(),null);
			
			bindEmailBean.setAction("BindLoginMail");
			bindEmailBean.setActionSource("1");
			bindEmailBean.setLoginId(user.get("loginId").toString());
			bindEmailBean.setLoginPassword("123456");
			bindEmailBean.setMail(existUser.getlBills().getEmail());
			bindEmailBean.setUserId((Integer)user.get("userId"));
			bindEmailCall.setData(bindEmailBean);
			bindEmailCall.callService();
			
			Logger.verifyEquals(ECode.ERROR400, bindEmailCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("false", bindEmailCall.getString("Success"), "验证Success");
			Logger.verifyEquals("邮箱已被他用户使用", bindEmailCall.getString("LastErrorMessage"), "验证Msg信息");
			Logger.verifyEquals("25", bindEmailCall.getString("Result"), "验证凭据（25表示邮箱已被他用户使用）");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BindEmail_005() {
		Logger.start(false,"不输入要绑定的邮箱，失败");
		try {
			Map user = UserCallService.registerUser(null, TestDataManager.getPhoneNum(),null);
			
			bindEmailBean.setAction("BindLoginMail");
			bindEmailBean.setActionSource("1");
			bindEmailBean.setLoginId(user.get("loginId").toString());
			bindEmailBean.setLoginPassword("123456");
			bindEmailBean.setUserId((Integer)user.get("userId"));
			bindEmailCall.setData(bindEmailBean);
			bindEmailCall.callService();
			
			Logger.verifyEquals(ECode.ERROR400, bindEmailCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("false", bindEmailCall.getString("Success"), "验证Success");
			Logger.verifyEquals("邮箱不能为空", bindEmailCall.getString("LastErrorMessage"), "验证Msg信息");
			Logger.verifyEquals("18", bindEmailCall.getString("Result"), "验证凭据（18表示邮箱为空）");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BindEmail_006() {
		Logger.start(false,"输入错误的邮箱格式，失败");
		try {
			Map user = UserCallService.registerUser(null, TestDataManager.getPhoneNum(),null);
			
			bindEmailBean.setAction("BindLoginMail");
			bindEmailBean.setActionSource("1");
			bindEmailBean.setLoginId(user.get("loginId").toString());
			bindEmailBean.setLoginPassword("123456");
			bindEmailBean.setUserId((Integer)user.get("userId"));
			bindEmailBean.setMail(TestDataManager.getRandomNum(4));
			bindEmailCall.setData(bindEmailBean);
			bindEmailCall.callService();
			
			Logger.verifyEquals(ECode.ERROR400, bindEmailCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("false", bindEmailCall.getString("Success"), "验证Success");
			Logger.verifyEquals("dto验证失败", bindEmailCall.getString("LastErrorMessage"), "验证Msg信息");
			Logger.verifyEquals("0", bindEmailCall.getString("Result"), "验证凭据（0表示邮箱格式错误）");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

}
